<template>
  <div>
    <div
      class="container"
      :style="{
        minHeight: '100vh',
        alignItems: 'center',
        // background:
        //   'url(http://codegen.caihongy.cn/20220804/31a88075584749efbcbae69f056a2943.png)',
        display: 'flex',
        width: '100%',
        backgroundSize: 'cover',
        backgroundPosition: 'center center',
        backgroundRepeat: 'no-repeat',
        justifyContent: 'center',
      }"
    >
      <el-form class="loginForm">
        <div v-if="true" class="title-container">校园访客预约管理系统登录</div>
        <div
          v-if="loginType == 1"
          class="list-item"
          :style="{
            width: '70%',
            margin: '10px auto 10px',
            alignItems: 'center',
            flexWrap: 'wrap',
            display: 'flex',
          }"
        >
          <!-- <div
            v-if="false"
            class="lable"
            :style="{
              width: '64px',
              lineHeight: '44px',
              fontSize: '14px',
              color: 'rgba(64, 158, 255, 1)',
            }"
          >
            用户名
          </div> -->
          <input
            class="loginInput"
            placeholder="请输入用户名"
            name="username"
            type="text"
            v-model="rulesForm.username"
          />
        </div>
        <div
          v-if="loginType == 1"
          class="list-item"
          :style="{
            width: '70%',
            margin: '10px auto 10px',
            alignItems: 'center',
            flexWrap: 'wrap',
            display: 'flex',
          }"
        >
          <!-- <div
            v-if="false"
            class="lable"
            :style="{
              width: '64px',
              lineHeight: '44px',
              fontSize: '14px',
              color: 'rgba(64, 158, 255, 1)',
            }"
          >
            密码：
          </div> -->
          <input
            class="loginInput"
            placeholder="请输入密码"
            name="password"
            type="password"
            v-model="rulesForm.password"
          />
        </div>
        <div
          :style="{ width: '90%', textAlign: 'center', margin: '20px auto' }"
          v-if="roles.length > 1"
          prop="loginInRole"
          class="list-type"
        >
          <el-radio
            v-for="item in roles"
            v-bind:key="item.roleName"
            v-model="rulesForm.role"
            :label="item.roleName"
            >{{ item.roleName }}</el-radio
          >
        </div>
        <div
          :style="{
            width: '100%',
            margin: '20px auto',
            alignItems: 'center',
            flexWrap: 'wrap',
            justifyContent: 'center',
            display: 'flex',
          }"
        >
          <div class="loginInBtDiv">
            <el-button
              class="loginInBt"
              v-if="loginType == 1"
              type="primary"
              @click="login()"
              >登录</el-button
            >
          </div>
          <el-button
            type="primary"
            @click="register('xuesheng')"
            class="register"
            >注册学生</el-button
          >
          <el-button
            type="primary"
            @click="register('jiaoshi')"
            class="register"
            >注册教师</el-button
          >
        </div>
      </el-form>
    </div>
  </div>
</template>
<script>
import menu from '@/utils/menu'
export default {
  data() {
    return {
      baseUrl: this.$base.url,
      loginType: 1,
      rulesForm: {
        username: '',
        password: '',
        role: '',
        code: '',
      },
      menus: [],
      roles: [],
      tableName: '',
      codes: [
        {
          num: 1,
          color: '#000',
          rotate: '10deg',
          size: '16px',
        },
        {
          num: 2,
          color: '#000',
          rotate: '10deg',
          size: '16px',
        },
        {
          num: 3,
          color: '#000',
          rotate: '10deg',
          size: '16px',
        },
        {
          num: 4,
          color: '#000',
          rotate: '10deg',
          size: '16px',
        },
      ],
    }
  },
  mounted() {
    let menus = menu.list()
    this.menus = menus

    for (let i = 0; i < this.menus.length; i++) {
      if (this.menus[i].hasBackLogin == '是') {
        this.roles.push(this.menus[i])
      }
    }
  },
  created() {
    this.getRandCode()
  },
  destroyed() {},
  components: {},
  methods: {
    //注册
    register(tableName) {
      this.$storage.set('loginTable', tableName)
      this.$storage.set('pageFlag', 'register')
      this.$router.push({ path: '/register' })
    },
    // 登陆
    login() {
      if (!this.rulesForm.username) {
        this.$message.error('请输入用户名')
        return
      }
      if (!this.rulesForm.password) {
        this.$message.error('请输入密码')
        return
      }
      if (this.roles.length > 1) {
        if (!this.rulesForm.role) {
          this.$message.error('请选择角色')
          return
        }

        let menus = this.menus
        for (let i = 0; i < menus.length; i++) {
          if (menus[i].roleName == this.rulesForm.role) {
            this.tableName = menus[i].tableName
          }
        }
      } else {
        this.tableName = this.roles[0].tableName
        this.rulesForm.role = this.roles[0].roleName
      }

      this.$http({
        url: `${this.tableName}/login?username=${this.rulesForm.username}&password=${this.rulesForm.password}`,
        method: 'post',
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.$storage.set('Token', data.token)
          this.$storage.set('role', this.rulesForm.role)
          this.$storage.set('sessionTable', this.tableName)
          this.$storage.set('adminName', this.rulesForm.username)
          this.$router.replace({ path: '/index/' })
        } else {
          this.$message.error(data.msg)
        }
      })
    },
    getRandCode(len = 4) {
      this.randomString(len)
    },
    randomString(len = 4) {
      let chars = [
        'a',
        'b',
        'c',
        'd',
        'e',
        'f',
        'g',
        'h',
        'i',
        'j',
        'k',
        'l',
        'm',
        'n',
        'o',
        'p',
        'q',
        'r',
        's',
        't',
        'u',
        'v',
        'w',
        'x',
        'y',
        'z',
        'A',
        'B',
        'C',
        'D',
        'E',
        'F',
        'G',
        'H',
        'I',
        'J',
        'K',
        'L',
        'M',
        'N',
        'O',
        'P',
        'Q',
        'R',
        'S',
        'T',
        'U',
        'V',
        'W',
        'X',
        'Y',
        'Z',
        '0',
        '1',
        '2',
        '3',
        '4',
        '5',
        '6',
        '7',
        '8',
        '9',
      ]
      let colors = [
        '0',
        '1',
        '2',
        '3',
        '4',
        '5',
        '6',
        '7',
        '8',
        '9',
        'a',
        'b',
        'c',
        'd',
        'e',
        'f',
      ]
      let sizes = ['14', '15', '16', '17', '18']

      let output = []
      for (let i = 0; i < len; i++) {
        // 随机验证码
        let key = Math.floor(Math.random() * chars.length)
        this.codes[i].num = chars[key]
        // 随机验证码颜色
        let code = '#'
        for (let j = 0; j < 6; j++) {
          let key = Math.floor(Math.random() * colors.length)
          code += colors[key]
        }
        this.codes[i].color = code
        // 随机验证码方向
        let rotate = Math.floor(Math.random() * 60)
        let plus = Math.floor(Math.random() * 2)
        if (plus == 1) rotate = '-' + rotate
        this.codes[i].rotate = 'rotate(' + rotate + 'deg)'
        // 随机验证码字体大小
        let size = Math.floor(Math.random() * sizes.length)
        this.codes[i].size = sizes[size] + 'px'
      }
    },
  },
}
</script>

<style lang="scss" scoped>
.container {
  min-height: 100vh;
  position: relative;
  background-repeat: no-repeat;
  background-position: center center;
  background-size: cover;
  // background: url(http://codegen.caihongy.cn/20220804/31a88075584749efbcbae69f056a2943.png);
  background: url(../assets/img/zhejiangligongdaxuetupian-10887913_1.jpg);
  // background: url(../assets/img/caochang.jpg);

  .loginForm {
    width: 600px;
    padding: 0px 0 20px;
    margin: 0;
    border-radius: 20px;
    background: rgba(255, 255, 255, 0.75);
    height: auto;

    .title-container {
      box-shadow: 0px 4px 10px 0px rgba(0, 0, 0, 0.302);
      padding: 50px 20px;
      margin: 0 auto 40px;
      color: #000;
      text-align: center;
      background: linear-gradient(
        180deg,
        #5286f7 0%,
        #799ff1 10%,
        rgba(152, 196, 238, 0.85) 30%,
        rgba(255, 255, 255, 0.9) 50%,
        rgba(152, 196, 238, 0.85) 70%,
        #7da5fc 90%,
        #5286f7 100%
      );
      width: 70%;
      line-height: 44px;
      font-size: 26px;
      font-weight: 600;
      border-radius: 15px;
    }

    .loginInput {
      border: 1px solid rgba(64, 158, 255, 1);
      padding: 0 10px;
      box-shadow: 0px 4px 10px 0px rgba(0, 0, 0, 0.302);
      color: #333;
      outline-offset: 4px;
      border-radius: 0;
      width: 100%;
      font-size: 14px;
      height: 44px;
    }

    .loginInBtDiv {
      display: flex;
      justify-content: center;
      width: 100%;
    }

    .loginInBt {
      margin: 0;
      border: 0;
      cursor: pointer;
      padding: 0 24px;
      outline: none;
      color: #fff;
      border-radius: 5px;
      background: rgba(59, 152, 240, 0.85);
      width: 53.5%;
      font-size: 18px;
      height: 50px;
    }

    .register {
      border: 0px solid rgba(64, 158, 255, 1);
      cursor: pointer;
      padding: 0 15px;
      margin: 10px;
      outline: none;
      color: rgb(255, 255, 255);
      border-radius: 4px;
      background: rgba(59, 152, 240, 0.7);
      width: 150px;
      font-size: 14px;
      height: 44px;
    }
  }

  .list-item /deep/ .el-input .el-input__inner {
    border: 0px solid rgba(64, 158, 255, 1);
    border-radius: 0;
    padding: 0 10px;
    box-shadow: 0px 4px 10px 0px rgba(0, 0, 0, 0.302);
    color: #333;
    width: 100%;
    font-size: 14px;
    outline-offset: 4px;
    height: 44px;
  }

  .list-code /deep/ .el-input .el-input__inner {
    border: 0px solid rgba(64, 158, 255, 1);
    border-radius: 0;
    padding: 0 10px;
    box-shadow: 0px 4px 10px 0px rgba(0, 0, 0, 0.302);
    outline: none;
    color: #333;
    width: 100%;
    font-size: 14px;
    height: 44px;
  }

  .list-type /deep/ .el-radio__input .el-radio__inner {
    background: rgba(53, 53, 53, 0);
    border-color: #666666;
  }
  .list-type /deep/ .el-radio__input.is-checked .el-radio__inner {
    background: rgba(59, 152, 240, 0.85);
    border-color: rgba(73, 186, 181, 1);
  }
  .list-type /deep/ .el-radio__label {
    color: #666666;
    font-size: 14px;
  }
  .list-type /deep/ .el-radio__input.is-checked + .el-radio__label {
    color: rgba(59, 152, 240, 0.85);
    font-size: 14px;
  }
}
</style>
